import Vue from 'vue'
import Vuex from 'vuex'
import {sessionStorage} from '../common/storage'

export const SET_USER_INFO = 'SET_USER_INFO'
// 获取用户信息
export const GET_USER_INFO = 'GET_USER_INFO'
// 判断是否登录
export const GET_IS_LOGIN = 'GET_IS_LOGIN'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userInfo: sessionStorage.get('user_info')
  },
  mutations: {
    [SET_USER_INFO] (state, userInfo) {
      state.userInfo = userInfo
      if (!userInfo) {
        sessionStorage.remove('user_info')
      } else {
        sessionStorage.set('user_info', userInfo)
      }
    }
  },
  actions: {
    [SET_USER_INFO] ({commit}, userInfo) {
      commit(SET_USER_INFO, userInfo)
    }
  },
  getters: {
    [GET_USER_INFO]: state => {
      return state.userInfo || {}
    },
    [GET_IS_LOGIN]: state => {
      !!state.userInfo && JSON.stringify(state.userInfo) !== '{}'
    }
  }
})


